<?php
//this first part concern the table containing the users own groups
if ((count($my_groups))!=0) {
	?>
	<table>
	<tr>
		<th colspan="3" style="text-align: center">My Groups</th>
	</tr>
	<tr>
		<th>Groups name</th>
		<th>Edit group members</th>
		<th>Delete</th>
	</tr>
	<?php foreach ($my_groups as $my_group): ?>
	<tr>
		<td><?php echo $my_group['groups']['group_name']; ?></td>
		<td><?php echo $this->Html->link('Edit', array('action' => 'edit', $my_group['groups']['id_group']))?></td>
		<td style="text-align: center"><?php echo $this->Form->postLink($this->Html->image("admin/delete.gif", array("alt" => "delete")), array('action' => 'delete', $my_group['groups']['id_group']), array('escape' => false, 'confirm' => 'Are you sure?')); ?></td>
	</tr>
	<?php endforeach;
	?>
	</table>
	<?php
}
?>
<br><hr><br>

<?php
//first we count the numbers of groups if we have 0 we have a message 
if ((count($groups))==0)
	echo "You are not a member of any other person's group.";
else { //in the other case we lists the groups and the users included in the groups
	?>
	<table>
		<?php
		$group_name='';
		foreach ($groups as $group):
			if ($group['groups']['group_name'] != $group_name) {
				//if the group name is the same as previously we are not creating a new row with the group name else we create a row with the group name
				$group_name = $group['groups']['group_name'];
				?>
				<tr>
					<th colspan=3 style="text-align: center">Group name: <?php echo $group['groups']['group_name']; ?></th>
				</tr>
				<tr>
					<th>Name</th>
					<th>Surname</th>
					<th>Email</th>
				</tr>
				<?php
			}
			?>
			<tr>
				<td><?php echo $group['users']['name']; ?></td>
				<td><?php echo $group['users']['surname']; ?></td>
				<td><?php echo $group['users']['email']; ?></td>
			</tr>
		<?php endforeach; ?>
	</table>
<?php } ?>

<br><br><hr><br>
<h3>Create a new group</h3>
<?php
//Create a new group
echo $this -> Form -> create('Group', array('action' => 'add'));
echo $this -> Form -> input('group_name', array('label' => 'Group name:'));
echo $this -> Form -> end('Save');
